package com.woniu.inlet.web.controller;

import com.woniu.exception.UserException;
import com.woniu.inlet.web.command.AddNoticeCommand;
import com.woniu.inlet.web.command.PayCommand;
import com.woniu.inlet.web.command.VerifyBalanceCommand;
import com.woniu.inlet.web.fo.UserFo;
import com.woniu.inlet.web.vo.UserVo;
import com.woniu.outlet.mysql.dao.UserDao;
import com.woniu.outlet.mysql.po.Notice;
import com.woniu.outlet.mysql.po.User;
import com.woniu.response.ResponseResult;
import com.woniu.service.UserService;
import com.woniu.service.domain.UserDo;
import com.woniu.utils.LoginPass;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@Api("用户功能相关测试")
@Slf4j
@CrossOrigin
public class UserController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UserService userService;

    @GetMapping("/user/payToken")
    //@ApiOperation("获取支付幂等性token")
    public ResponseResult<Object> getPayToken() {
        String payToken = UUID.randomUUID().toString().replace("-", "");
        //测试时候 设置存活时间  -->上线删除
        stringRedisTemplate.opsForValue().set(payToken, payToken, 60 * 15, TimeUnit.SECONDS);
        return ResponseResult.ok(payToken);
    }

    //基金 黄金 定期活期 产品的正常买入
    @PostMapping("/user/pay")
    // @ApiOperation("用户购买产品支付测试")
    public ResponseResult<Object> userPay(@RequestBody UserFo userFo) {
        try {
            PayCommand payCommand = new PayCommand(userFo);
            payCommand.execute();
            return ResponseResult.ok("支付成功！！！");
        } catch (Exception e) {
            if (e instanceof UserException) {
                return ResponseResult.fail(e.getMessage());
            } else {
                e.printStackTrace();
                return ResponseResult.fail("支付系统维护中，请稍后再试！！！");
            }
        }
    }

    @PostMapping("/user/verifyBalance")
    // @ApiOperation("定投余额验证是否充足")
    public boolean verifyBalance(
            @RequestParam("userId") Integer userId,
            @RequestParam("money") BigDecimal money,
            @RequestParam("fixedId") Integer fixedId) {
        VerifyBalanceCommand command = new VerifyBalanceCommand(userId, money, fixedId);
        boolean b = command.execute();
        return b;
    }


    @GetMapping("/user/{id}")
    @ApiOperation("根据用户id查询测试")
    public Object getById(@PathVariable("id") Integer id) {
        try {
            UserDo userDo = userService.getById(id);
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(userDo, userVo);
            return userVo;
        } catch (Exception e) {
            if (e instanceof UserException)
                return ResponseResult.ok(e.getMessage());
            else
                return ResponseResult.fail("系统维护中");
        }
    }

    @GetMapping("/user/ids")
    @ApiOperation("根据id查询多个用户是否存在")
    public boolean isExist(@RequestParam("ids") List<Integer> ids) {
        try {
            boolean flag = userService.isExist(ids);
            return flag;
        } catch (Exception e) {
            return false;
        }

    }

    @GetMapping("/user/id")
    @ApiOperation("自定义注解拦截未登录验证")
    @LoginPass
    public Object loginPass(Integer id, String token) {
        try {
            UserDo userDo = userService.getById(id);
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(userDo, userVo);
            return userVo;
        } catch (Exception e) {
            if (e instanceof UserException)
                return ResponseResult.ok(e.getMessage());
            else
                return ResponseResult.fail("系统维护中");
        }
    }

}
